package com.sky.mapper;

import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import com.sky.vo.OrdersVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author 42
 * @date 2025/4/1
 * @Description
 */
@Mapper
public interface OrderMapper {
    /**
     * 插入订单数据
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 用于替换微信支付更新数据库状态的问题
     * @param orderStatus
     * @param orderPaidStatus
     */
    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{check_out_time} " +
            "where number = #{orderNumber}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, String orderNumber);


    List<OrdersVO> list(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 统计数量
     * @param toBeConfirmed
     * @return
     */
    @Select("SELECT COUNT(*) FROM orders WHERE status = #{toBeConfirmed}")
    Integer getOrderStatusNum(Integer toBeConfirmed);

    /**
     * 根据id查询订单信息
     * @param id
     * @return
     */
    @Select("SELECT * FROM orders WHERE id = #{id}")
    Orders getById(Long id);

    List<OrderVO> getOrderList(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 扫描超时时间
     * @param unPaid
     * @param dateTime
     * @return
     */
    @Select("SELECT * FROM orders WHERE status = #{unPaid} AND order_time < #{dateTime} ")
    List<Orders> getByStatusAndOrderTime(Integer unPaid, LocalDateTime dateTime);

    /**
     * 统计营业额
     * @param map
     * @return
     */
    Double sumByMap(Map<String, Object> map);


    Integer getOrderCount(@Param("min") LocalDateTime min, @Param("max") LocalDateTime max, @Param("status") Integer status);

    /**
     * 统计top10的销量数据
     * @param min
     * @param max
     * @return
     */
    List<GoodsSalesDTO> getTop10Statistics(@Param("min") LocalDateTime min, @Param("max") LocalDateTime max);

    /**
     * 根据动态条件统计订单数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
